package com.kxz.core.web.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.kxz.core.po.User;
import com.kxz.core.service.UserService;

@Controller
public class UserController {

	@Autowired
	private UserService userService;

	// 用户登录
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	public String login(String usercode, String password, Model model, HttpSession session) {
		// 通过账号密码查询用户
		User user = userService.findUser(usercode, password);
		// user是否为空
		if (user != null) {
			// 存入session域
			session.setAttribute("USER_SESSION", user);
			// 重定向:在进入该页面时就默认显示数据

			return "benchi1";
			// return "customer"; 这是没有携带数据的，登录进去显示空页面
		}
		model.addAttribute("msg", "账号密码错误，请重新输入!!");
		return "login";
	}

	
	
	 // 用户注册
	  
	 @RequestMapping(value = "/register.action",method = RequestMethod.POST)
	  
	  
	  public String register(String usercode,String username,String password, Model model) { 
		 User user2 = userService.findUsercode(usercode);
			// user是否为空
			if (user2 == null) {
				userService.addUser(usercode,username,password);
				model.addAttribute("msg", "注册成功!!");
				return "register";
				
			}else {
				model.addAttribute("msg", "账号已存在，请重新输入");
				return "register";
			}
			
	  
	  }
	  
	  // 用户注册
	  
	  @RequestMapping(value = "/register.action",method = RequestMethod.GET)
	 
	  public ModelAndView register() {
		  return new ModelAndView(); 
		  }
	  
	  
	

	@RequestMapping(value = "/login/benchi1.action")
	public String tobenchi1() {
		return "benchi1";
	}
	@RequestMapping(value = "/login/benchi2.action")
	public String tobenchi2() {
		return "benchi2";
	}
	@RequestMapping(value = "/login/benchi3.action")
	public String tobenchi3() {
		return "benchi3";
	}
	@RequestMapping(value = "/login/benchi4.action")
	public String tobenchi4() {
		return "benchi4";
	}
	@RequestMapping(value = "/login/benchi5.action")
	public String tobenchi5() {
		return "benchi5";
	}

	// 退出登录
	@RequestMapping(value = "/logout.action")
	public String logout(HttpSession session) {
		// 清除session
		session.invalidate();
		// 重定向到登录页面
		return "redirect:login.action";
	}

	// 向用户登录页面跳转
	@RequestMapping(value = "/login.action", method = RequestMethod.GET)
	public String login() {
		return "login";
	}

}
